package com.alex.demos.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.alex.common.persistent.BaseDao;
import com.alex.demos.domain.GreetingType0;
import com.alex.demos.domain.SayingPO;

@Repository
public class SayingDao extends BaseDao {

	public void insertSaying(final SayingPO dto) {
		dao(new Run<Boolean>() {
			protected Boolean run(EntityManager em) {
				em.persist(dto);
				return true;
			}
		}, false);
	}

	private static final String QuerySQL4FEEDBACK = "from "
			+ SayingPO.class.getName() + " tb where tb.type0="
			+ GreetingType0.FEEDBACK.ordinal() + " order by date desc";

	@SuppressWarnings("unchecked")
	public List<SayingPO> listGreeting(final int first, final int max) {
		return (List<SayingPO>) dao(new Run<List<SayingPO>>() {

			protected List<SayingPO> run(EntityManager em) {
				Query query = em.createQuery(QuerySQL4FEEDBACK);
				query.setFirstResult(first);
				query.setMaxResults(max);
				List<SayingPO> rt = query.getResultList();
				rt.size();
				return rt;
			}
		}, true);
	}
}
